package com.java.al.swing;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class TreeChangeColor {
 public TreeChangeColor() {
 }
 public static void main(String[] args) {
   JFrame frame = new JFrame();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   frame.getContentPane().add(new TreeArea());
   frame.pack();
   frame.show();
 }

}

/**
* <p>Title: TreeArea</p>
* <p>Description:  Creat Tree.</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
class TreeArea
   extends JScrollPane{

 Dimension minSize = new Dimension(100, 100);

 //把节点信息保存到树中
 JTree tree;
 //保存当前选中的树节点
 JPopupMenu popupMenu = new JPopupMenu();

 public TreeArea() {
 JMenuItem jmi = new JMenuItem("改变字体颜色");
 jmi.addActionListener(    new ActionListener() {
     public void actionPerformed(ActionEvent e) {
       TreePath tp = tree.getSelectionPath();
       if(tp != null)
       changeNode(tp.getLastPathComponent());
     }
   });
   popupMenu.add(jmi);

   TreeNode rootNode = createNodes();
   tree = new JTree(rootNode);
   tree.setCellRenderer(new TreeCellRenderer());
   setViewportView(tree);
   tree.addMouseListener(new MouseAdapter() {
     public void mousePressed(MouseEvent e) {
       popupMenu.show(e.getComponent(), e.getX(), e.getY());
     }
   });
 }
 
 void changeNode(Object obj) {
   DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode)obj;
   
   Object[] sytle = (Object[])((ComboBoxItem)dmtn.getUserObject()).getValue();
   Color fgColor = (Color)sytle[1];
     fgColor = JColorChooser.showDialog(this, "选择前景色", fgColor);
     if (fgColor != null)
       sytle[1] = fgColor;
   repaint();
   
 }

 /**
  * Create Tree Node and return the tree root.
  * @return TreeNode
  */
 private TreeNode createNodes() {
   DefaultMutableTreeNode deviceNodeRoot = getRoot();
   creatNodes(deviceNodeRoot, null);
   return deviceNodeRoot;
 }

 public Dimension getMinimumSize() {
   return minSize;
 }

 public Dimension getPreferredSize() {
   return minSize;
 }

 /**
  * 获得树的根节点
  * @return Object
  */
 public DefaultMutableTreeNode getRoot() {
   ComboBoxItem cbi = new ComboBoxItem(new Object[]{new Font(getFont().getName(), Font.PLAIN, 12), Color.red}, "root");
   DefaultMutableTreeNode root = new DefaultMutableTreeNode(cbi);
   return root;
 }

 /**
  * 创建根下面的所有树节点，使用递归
  * @param parentNode DefaultMutableTreeNode
  * @param parentId int
  */
 protected void creatNodes(DefaultMutableTreeNode parentNode, String parentId) {
   ComboBoxItem cbi1 = new ComboBoxItem(new Object[]{new Font(getFont().getName(), Font.PLAIN, 12), Color.red}, "ch1");
   ComboBoxItem cbi2 = new ComboBoxItem(new Object[]{new Font(getFont().getName(), Font.PLAIN, 13), Color.pink}, "ch2");
   ComboBoxItem cbi3 = new ComboBoxItem(new Object[]{new Font(getFont().getName(), Font.PLAIN, 14), Color.orange}, "ch3");
   ComboBoxItem cbi4 = new ComboBoxItem(new Object[]{new Font(getFont().getName(), Font.PLAIN, 15), Color.yellow}, "ch4");
   ComboBoxItem cbi5 = new ComboBoxItem(new Object[]{new Font(getFont().getName(), Font.PLAIN, 16), Color.magenta}, "ch5");
   ComboBoxItem cbi6 = new ComboBoxItem(new Object[]{new Font(getFont().getName(), Font.PLAIN, 17), Color.green}, "ch6");
   DefaultMutableTreeNode dmt1 = new DefaultMutableTreeNode(cbi1);
   DefaultMutableTreeNode dmt2 = new DefaultMutableTreeNode(cbi2);
   DefaultMutableTreeNode dmt3 = new DefaultMutableTreeNode(cbi3);
   DefaultMutableTreeNode dmt4 = new DefaultMutableTreeNode(cbi4);
   DefaultMutableTreeNode dmt5 = new DefaultMutableTreeNode(cbi5);
   DefaultMutableTreeNode dmt6 = new DefaultMutableTreeNode(cbi6);
   parentNode.add(dmt1);
   parentNode.add(dmt2);
   parentNode.add(dmt3);
   dmt4.add(dmt5);
   dmt4.add(dmt6);
   parentNode.add(dmt4);
 }
}
class TreeCellRenderer
   extends DefaultTreeCellRenderer {
 public Component getTreeCellRendererComponent(JTree tree, Object value,
                                               boolean selected,
                                               boolean expanded, boolean leaf,
                                               int row,
                                               boolean hasFocus) {
    super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

   if (value instanceof DefaultMutableTreeNode) {
     DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
     ComboBoxItem userObject = (ComboBoxItem)node.getUserObject();
     Object[] style = (Object[])userObject.getValue();
     setFont((Font)style[0]);
     setForeground((Color)style[1]);
   }
   return this;
 }
}

class ComboBoxItem {
   private Object value; //真实值
   private String showString; //显示值

   public ComboBoxItem(Object value, String showString)
   {
       this.value = value;
       this.showString = showString;
   }

   public ComboBoxItem(Object value)
   {
       this(value, value.toString());
   }

   public ComboBoxItem(String value)
   {
       this(value, value);
   }

   public ComboBoxItem()
   {
       this(null, "");
   }

   public void setValue(Object value)
   {
       this.value = value;
   }

   public void setShowString(String showString)
   {
       this.showString = showString;
   }

   public String toString()
   {
       return showString;
   }

   public String getShowString()
   {
       return showString;
   }

   public Object getValue()
   {
       return value;
   }
}
